package com.knowvoyage.knowvoyageserver.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.knowvoyage.knowvoyageserver.mapper.QuestionMapper;
import com.knowvoyage.knowvoyageserver.pojo.entity.Question;
import com.knowvoyage.knowvoyageserver.pojo.vo.QuestionPageVO;
import com.knowvoyage.knowvoyageserver.service.QuestionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Supplier;

@Service
@Slf4j
@RequiredArgsConstructor
public class QuestionServiceImpl implements QuestionService {
    private final QuestionMapper questionMapper;

//    @Override
//    public PageInfo<Question> getQuestionsByCondition(
//            Integer type, Integer subject, Integer year,
//            int pageNum, int pageSize
//    ) {
//        // 关键代码：启动分页（必须在查询前调用）
//        PageHelper.startPage(pageNum, pageSize);
//
//        // 执行原有查询（PageHelper 自动拦截并分页）
//        List<Question> questions = questionMapper.selectByCondition(type, subject, year);
//
//        // 包装分页结果（包含总条数、总页数等信息）
//        return new PageInfo<>(questions);
//    }
@Override
public QuestionPageVO getQuestionsByCondition(Integer type, Integer subject, Integer year, int pageNum, int pageSize) {
    // 分页查询
    PageHelper.startPage(pageNum, pageSize);
    List<Question> questions = questionMapper.selectByCondition(type, subject, year);
    PageInfo<Question> pageInfo = new PageInfo<>(questions);
    // 统计当前科目总题数（若未指定科目则统计全部）
    Integer subjectTotal = questionMapper.countBySubject(subject);
    if(subjectTotal == null) {
        subjectTotal = 0; // 处理可能的null值
    }
    Integer yearTotal = questionMapper.countByYear(year);
    if(yearTotal == null) {
        yearTotal = 0; // 处理可能的null值
    }
    Integer typeTotal = questionMapper.countByType(type);
    if(typeTotal == null) {
        typeTotal = 0; // 处理可能的null值
    }

    // 构建VO对象
    QuestionPageVO vo = new QuestionPageVO();
    vo.setList(pageInfo.getList());
    vo.setPageNum(pageInfo.getPageNum());
    vo.setPageSize(pageInfo.getPageSize());
    vo.setTotal(questionMapper.countAll());
    vo.setPages(pageInfo.getPages());
    vo.setSubjectTotal(subjectTotal);
    vo.setYearTotal(yearTotal);
    vo.setTypeTotal(typeTotal);

    return vo;
}


    public QuestionPageVO getQuestionsByCondition1(Integer type, Integer subject, Integer year, int pageNum, int pageSize) {
        // 1. 分页查询题目
        PageInfo<Question> pageInfo = getPaginatedQuestions(type, subject, year, pageNum, pageSize);

        // 2. 获取统计数据
        int subjectTotal = getCountOrDefault(() -> questionMapper.countBySubject(subject));
        int yearTotal = getCountOrDefault(() -> questionMapper.countByYear(year));
        int typeTotal = getCountOrDefault(() -> questionMapper.countByType(type));
        int total = getCountOrDefault(() -> questionMapper.countAll());

        // 3. 构建VO对象
        return buildQuestionPageVO(pageInfo, subjectTotal, yearTotal, typeTotal, total);
    }

    private PageInfo<Question> getPaginatedQuestions(Integer type, Integer subject, Integer year, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Question> questions = questionMapper.selectByCondition(type, subject, year);
        return new PageInfo<>(questions);
    }

    private int getCountOrDefault(Supplier<Integer> supplier) {
        Integer result = supplier.get();
        return result != null ? result : 0;
    }

    private QuestionPageVO buildQuestionPageVO(PageInfo<Question> pageInfo, int subjectTotal, int yearTotal, int typeTotal, int total) {
        QuestionPageVO vo = new QuestionPageVO();
        vo.setList(pageInfo.getList());
        vo.setPageNum(pageInfo.getPageNum());
        vo.setPageSize(pageInfo.getPageSize());
        vo.setPages(pageInfo.getPages());
        vo.setSubjectTotal(subjectTotal);
        vo.setYearTotal(yearTotal);
        vo.setTypeTotal(typeTotal);
        vo.setTotal(total);
        return vo;
    }

}
